<template>
	<view class="content">
		<view class="psit-head">
			<view class="psit-head-left">
				<view class="psit-head-tit" v-if="city">{{city}}</view>
				<view class="psit-head-tit" v-else>请选择</view>
				<image src="../../../static/position-right.png"></image>
			</view>
			<view class="psit-head-right" @click="cancelPosi()">取消</view>
		</view>
		<view class="psit-bot">
			<view class="psit-city">
				<view class="psit-city-tit">当前定位城市</view>
				<view class="psit-city-cont">
					<view class="psit-city-cont-box">
						<image class="psit-city-icon" src="../../../static/position.png"></image>
						<view class="psit-city-name">广州</view>
					</view>
					<view class="anew-psit">重新定位</view>
				</view>
				<view class="psit-city">
					<view class="psit-city-tit">历史浏览城市</view>
					<view class="psit-city-name-s">
						<view class="psit-city-name">广州</view>
						<view class="psit-city-name">北京</view>
					</view>
				</view>
				<view class="psit-city">
					<view class="psit-city-tit">热门城市X</view>
					<view class="psit-city-name-s">
						<view class="psit-city-name">北京</view>
						<view class="psit-city-name">上海</view>
						<view class="psit-city-name">广州</view>
						<view class="psit-city-name">深圳</view>
						<view class="psit-city-name">武汉</view>
					</view>
				</view>
				<view class="psit-city" v-for="item,i in cityList" :key="i" :id=item.f>
					<view class="psit-city-tit" >{{ item.f }}</view>
					<view class="psit-city-name-box" style="display: flex; flex-wrap: wrap; margin-left: 68upx;">
						<view class="psit-city-names" v-for="item2,ii in item.f_city" :key="ii">
							<view class="psit-city-name-s" :data-id="item2.id" :data-cname="item2.cname" @click="checkCity" style="color: #575757; width: 159upx; margin: 28upx 0;">
								{{ item2.cname }}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="psit-right">
			<view class="mar-bot">#</view>
			<view class="mar-bot">热</view>
			<view class="psit-right-item" 
				  id="arc-content" 
				  v-for="item,i in list.place_pingyin_f" 
				  :key="i"
				>
				<view @click="AppointedSubject" class="mar-bot" :data-id=item id="letter-scroll">{{ item }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:{},
				city:'',
				cityList:{},
			}
		},
		onLoad() {
			this.getData()
			this.getAreaData()
			uni.getStorage({
				key:'setCity',
				success:(e)=>{
					this.city=e.data.cname
				}
			})
		},
		methods: {
			cancelPosi(){
				if(this.city == ''){
					uni.showToast({
						title:'请先选择城市',
						duration:1500,
						icon:'none'
					})
				} else {
					uni.showModal({
						title: '温馨提示',
						content: '确定取消选择城市吗？',
						success: (res) => {
							if (res.confirm) {
								uni.removeStorage({
									key: 'setCity',
								})
								location.reload()
							}
						}
					})
				}
			},
			checkCity(e){
				var _this=this
				var tem_arr=e.target.dataset
				uni.showModal({
					title: '温馨提示',
					content: '确定选择 '+tem_arr.cname+' 吗？',
					success: function (res) {
						if (res.confirm) {
							uni.setStorage({
								key:'setCity',
								data:e.target.dataset,
								success:(e)=>{
									_this.city=tem_arr.cname
									uni.$emit("updateCity",{city: _this.city})
									uni.pageScrollTo({
										scrollTop: 0,
										duration:300
									})
								}
							})
							uni.switchTab({
								url:'../index'
							})
						} else if (res.cancel) {
							console.log(res.cancel)
						}
					}
				})
			},
			async getData() {
				const res = await this.get('/Index/index/config')
				if(res.code == 200){
					this.list = res.data
				}
			},
			async getAreaData() {
				const res = await this.get('/Index/Index/get_area')
				if(res.code == 200){
					this.cityList = res.data
				}
			},
			AppointedSubject(e) {
				var thisID = e.currentTarget.dataset.id;
				uni.createSelectorQuery().select("#"+thisID).boundingClientRect(data=>{//目标位置的节点：类class或者id
					uni.createSelectorQuery().select(".content").boundingClientRect(res=>{//最外层盒子的节点：类class或者id
						uni.pageScrollTo({
							duration: 300,//过渡时间
							scrollTop:data.top - res.top,//到达距离顶部的top值
						})
					}).exec()
				}).exec();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		.psit-head{
			display: flex;
			justify-content: space-between;
			padding: 24upx 34upx 26upx 40upx;
			.psit-head-left{
				display: flex;
				.psit-head-tit{
					color: #727377;
					margin-right: 10upx;
				}
				image{
					width: 40upx;
					height: 40upx;
				}
			}
			.psit-head-right{
				color: #AAAAAA;
			}
		}
		.psit-bot{
			margin: 22upx 24upx 0 22upx;
			.psit-city{
				.psit-city-tit{
					height: 70upx;
					color: #AAAAAA;
					font-size: 24upx;
					background: #F6F7FB;
					padding: 26upx 0 0 26upx;
				}
				.psit-city-cont{
					display: flex;
					justify-content: space-between;
					padding: 30upx 40upx 28upx 30upx;
					.psit-city-cont-box{
						display: flex;
						.psit-city-icon{
							width: 36upx;
							height: 36upx;
							color: #575757;
						}
					}
					.anew-psit{
						color: #528FFF;
					}
				}
				.psit-city-name-s{
					display: flex;
					flex-wrap: wrap;
					margin-left: 68upx;
					.psit-city-name{
						width: 159upx;
						color: #575757;
						margin: 28upx 0;
					}
				}
			}
		}
		.psit-right{
			top: 200upx;
			right: 32upx;
			position: fixed;
			text-align: center;
			.psit-right-item{
				.mar-bot{
					color: #6F6F6F;
					font-size: 24upx;
					margin-bottom: 8upx;
				}
			}
		}
	}
</style>
